<template>
  <div>
    <van-search v-model="keyWords" background="#4fc08d" show-action label="关键字" placeholder="请输入搜索关键词"
      @search="onSearch">
      <template #action>
        <div @click="onSearch">搜索</div>
      </template>
    </van-search>
    <div class="hisBox">
      <h5>搜索历史记录</h5>
      <span class="his" v-for="h in searchHistory" :key="h" @click="clickSearchHis(h)">
        {{ h }}
      </span>
    </div>
    <div class="items">
      <div class="item" v-for="item in items" @click="enterDetail(item.id)">
        <img :src="item.pic" alt="">
        <div class="item-info">
          <h5>{{ item.name }}</h5>
          ￥<span>{{ item.originalPrice }}</span>
          <van-icon @click.stop="addItemCart(item.id)" class="addCart" class-prefix="icon" name="gouwuchetianjia"
            size="20PX" />
        </div>
      </div>
    </div>
  </div>
  <CommonTabbar />
</template>

<script setup lang="ts" name="search">
import { ref } from 'vue'
import { fetchList, addCart } from '@a/index'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import CommonTabbar from '@/components/CommonTabbar.vue';
//关键字
const keyWords = ref<string>('')
//搜索历史记录
let searchHistory: any[] = localStorage.getItem('his') ? JSON.parse(localStorage.getItem('his')) : []
//搜索事件
const onSearch = () => {
  // alert(1)
  searchHistory.unshift(keyWords.value)
  searchHistory = [...new Set(searchHistory)]
  if (searchHistory.length > 5) {
    searchHistory.pop()
  }
  localStorage.setItem('his', JSON.stringify(searchHistory))
  getItems()
}
//点击历史记录搜索
const clickSearchHis = (h: string) => {
  keyWords.value = h
  onSearch()
}
//搜索列表请求
const items = ref<any[]>([])
const getItems = () => {
  fetchList({
    k: keyWords.value
  }).then(res => {
    console.log(res);
    if (res.data.code === 0) {
      items.value = res.data.data.result
    }
    if (res.data.code === 700) {
      items.value = []
    }
  })
}
//跳转详情
const router = useRouter()
const enterDetail = (id: number) => {
  router.push({
    path: '/details',
    query: {
      id
    }
  })
}
//加入购物车
const addItemCart = (goodId: number) => {
  addCart({
    goodsId: goodId,
    number: 1
  }).then(res => {
    console.log(res);
    if (res.data.code === 0) {
      // 加入购物车成功
      showToast({
        message: '加入购物车成功'
      })
    }
  })
}

</script>

<style lang="scss" scoped>
.items {
  background-color: #f1f1f1;

  .item {
    height: 110px;
    display: flex;
    padding: 10px;
    background-color: #fff;
    margin-top: 10px;

    img {
      width: 4rem;
    }

    .item-info {
      flex: 1;
      padding: 0 10px;

      h5 {
        width: 5rem;
        line-height: 40px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        padding-top: 5px;
      }

      span {
        font-size: 20PX;
        color: #ec1109;
      }

      .addCart {
        /* margin-left: 100PX; */
        float: right;
        margin-right: 5px;
      }

      .item-action {
        display: flex;
        justify-content: space-between;

        p {
          font-size: 20PX;
          color: #ec1109;
        }

      }
    }
  }
}

.hisBox {
  padding-left: 10px;
}

h5 {
  margin-bottom: 10px;
}

.his {
  margin-right: 10px;
  margin-bottom: 10px;
  background-color: #f1f1f1;
  display: inline-block;
  border: 1px solid #999;
  border-radius: 20px;
  /* height: 20PX; */
  line-height: 20px;
  padding: 0 5px;
}
</style>